using System.Text.RegularExpressions;

namespace Gateway.Common.Extensions;

public class ReplaceHtmlHelper
{
     private IList<Regex> _regexs = new List<Regex>();
        private IList<string> _replacement = new List<string>();

        private static ReplaceHtmlHelper _replaceHtml = null;
        private static readonly object _object = new object();
        private ReplaceHtmlHelper() { }
        public static ReplaceHtmlHelper Instance
        {
            get
            {
                if (_replaceHtml == null)
                {
                    lock (_object)
                    {
                        if (_replaceHtml == null)
                        {
                            _replaceHtml = SetInstance(new ReplaceHtmlHelper());
                        }
                    }
                }
                return _replaceHtml;
            }
        }

        public Regex GetRegex(int count)
        {
            return _regexs[count];
        }
        public string GetReplacement(int count)
        {
            return _replacement[count];
        }
        public int GetReplacementCount()
        {
            return _replacement.Count;
        }
        public string ReplaceHtmlTag(string Htmlstring)
        {
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Regex aRegex = null;
            for (int count = 0; count < this.GetReplacementCount(); count++)
            {
                aRegex = this.GetRegex(count);
                if (aRegex != null)
                {
                    Htmlstring = aRegex.Replace(Htmlstring, this.GetReplacement(count), -1, 0);
                }
            }
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }

        private void AddRegex(Regex aRegex, string Replacement)
        {
            _regexs.Add(aRegex);
            _replacement.Add(Replacement);
        }

        private static ReplaceHtmlHelper SetInstance(ReplaceHtmlHelper aReplaceHtml)
        {
            #region 赋值正则表达式和替换后的字符数组
            string[] pattern = new string[]
            {
                @"<script.*?</script>",@"<style.*?</style>",@"<.*?>",
                @"<(.[^>]*)>",@"([\r\n])[\s]+",@"-->",
                @"<!--.*",@"&(quot|#34);",@"&(amp|#38);",
                @"&(lt|#60);",@"&(gt|#62);",@"&(nbsp|#160);",
                @"&(iexcl|#161);",@"&(cent|#162);",@"&(pound|#163);",
                @"&(copy|#169);",@"&#(\d+);"
            };
            string[] replacement = new string[]
            {
                "","","","","","","","\"","&","<",">","","\xa1","\xa2","\xa3","\xa9",""
            };
            #endregion

            if (pattern.Length != replacement.Length)
            {
                throw new Exception("正则表达式数组和替换后的字符数组的长度不一致！");
            }

            int count = 0; //计数器
            foreach (string str in pattern)
            {
                Regex aRegex = new Regex(str);
                aReplaceHtml.AddRegex(aRegex, replacement[count]);
                count += 1;
            }
            return aReplaceHtml;
        }
}